<template>
  <div class="recommend">
    <div class="recommend-item" v-for="item in recommends.slice(0, picture)" :key="item.id">
      <a href="" @click.prevent="toDetail(item)">
        <img v-lazy="item.cover_url" alt="" @load="imageLoad" />
        <!-- <img src="https://img01.yzcdn.cn/vant/ipad.jpeg" alt="" @load="imageLoad" /> -->
        <div>{{ item.title }}</div>
      </a>
    </div>
  </div>
</template>

<script>
import { getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
export default {
  name: 'RecommedView',
  props: {
    recommends: {
      type: Array,
      default() {
        return []
      }
    }
  },
  setup() {
    let Launches = 0
    let picture = 4
    // 获取当前的路由实例
    const router = useRouter()
    // 获取当前的组件实例
    const { proxy: _this } = getCurrentInstance()
    function toDetail({ id }) {
      router.push({
        name: 'detail',
        params: {
          id
        }
      })
    }
    // 监听图片加载完毕事件
    function imageLoad() {
      // 当图片加载到第四次的时候触发事件
      if (++Launches === picture) {
        // 事件总线发射
        _this.$bus.emit('imageLoad')
      }
    }
    return {
      toDetail,
      imageLoad,
      picture
    }
  }
}
</script>

<style lang="scss" scoped>
.recommend {
  display: flex;
  width: 100vw;
  padding: 0.15rem 0 0.3rem;
  border-bottom: 0.08rem solid #eee;
  font-size: 12px;
  .recommend-item {
    text-align: center;
    flex: 1;
    img {
      width: 70px;
      height: 70px;
      margin-bottom: 0.1rem;
    }
  }
}
</style>